package com.jojo.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jojo.store.entity.Address;
import com.jojo.store.entity.User;
import com.jojo.store.service.IAddressService;
import com.jojo.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addresses")
public class AddressController extends BaseController{
    @Autowired
    private IAddressService addressService;

    @RequestMapping("/add_new_address")
    public JsonResult<Void> addAddress(HttpSession session, Address address){
        Integer uid = getUidBySession(session);
        String uname = getUnmBySession(session);
        addressService.addAddress(address,uid,uname);
        return new JsonResult<>(OK);
    }
    @RequestMapping({"","/"})
    public JsonResult<List<Address>> allAddressByUId(HttpSession session){
//        Page<Object> page = new Page<>();
        String uname = getUnmBySession(session);
        Integer uid = getUidBySession(session);
        List<Address> data = addressService.findByuidAddress(uid);
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("{aid}/set_default")
    public JsonResult<Void> changeDefault(@PathVariable("aid") Integer aid, HttpSession session){
        Integer uid = getUidBySession(session);
        String uname = getUnmBySession(session);
        addressService.changeDefault(uid,aid,uname);
        return new JsonResult<Void>(OK);
    }
   @RequestMapping("{aid}/delete")
    public  JsonResult<Void> deleteAddress(@PathVariable("aid")Integer aid,HttpSession session){
       String unmBySession = getUnmBySession(session);
       Integer uidBySession = getUidBySession(session);
       addressService.deleteAndDef(uidBySession,aid,unmBySession);
       return new JsonResult<Void>(OK);

   }

}
